package za.co.johanmynhardt.jsf.bean;

import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import za.co.johanmynhardt.jee.entity.Post;
import za.co.johanmynhardt.jee.service.PostService;

/**
 * @author johan.mynhardt@thumbtribe.mobi
 * @version 2011.0722
 *          Time: 10:18 PM
 */
@ManagedBean
@RequestScoped
public class PostCommentBean {
	private Logger logger = Logger.getLogger(PostCommentBean.class.getSimpleName());
	Long postId = null;
	Post post = null;
	String content = null;

	boolean viewingComments;

	@EJB
	private PostService postService;

	public Long getPostId() {
		return postId;
	}

	public void setPostId(Long postId) {
		this.postId = postId;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public boolean isViewingComments() {
		return viewingComments;
	}

	public void setViewingComments(boolean viewingComments) {
		this.viewingComments = viewingComments;
	}

	public void viewComment(ActionEvent actionEvent) {
		this.setViewingComments(true);
		UIParameter component = (UIParameter) actionEvent.getComponent().findComponent("postId");
		logger.info(String.format("Trying to view comments for post:  (%s)", component.getValue().toString()));

		Post post = postService.getPostById(Long.parseLong(component.getValue().toString()));

		if (post != null) {
			this.postId = post.getId();
			this.post = post;
			logger.info(String.format("Comments(%s) for post(%s): %s", post.getComments().size(), post.getId(), post.getComments()));
		}

		//Long viewPostId = Long.parseLong(((UIParameter)actionEvent.getComponent().findComponent("postId")).getValue().toString());
		//logger.info(String.format("Viewing comments for post(%s) (viewPostId: %s)", postId, viewPostId));
	}
}
